<template>
  <el-dialog
    v-model="dialogVisible"
    title="审核评论"
    width="430"
    class="qc-custom-dialog"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="70px"
      class="dialog-form-wrap"
    >
      <el-form-item label="评价内容">
        <div class="text-all">{{ ruleForm.content }}</div>
      </el-form-item>
      <el-form-item label="审核评论" prop="spectacleStatus" class="has-tips">
        <el-radio-group
          v-model="ruleForm.spectacleStatus"
          v-for="(item, index) in optionsS.status"
          :key="index"
        >
          <el-radio :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
        <div class="tip">
          <div class="icon"></div>
          {{
            ruleForm.spectacleStatus === '2'
              ? '展示：评论信息在移动端公开展示；关闭：评论信息仅对评论用户本人展示'
              : '展示：评论信息在移动端公开展示；关闭：评论信息仅对评论用户本人展示'
          }}
        </div>
      </el-form-item>
      <el-form-item label="评价回复">
        <el-input
          v-model.trim="ruleForm.reply"
          placeholder="请输入评价回复"
          maxlength="200"
          type="textarea"
          :rows="3"
          show-word-limit
          clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="hide">取消</el-button>
        <el-button type="primary" v-btnAntiShake="onSubmit"> 保存 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup name="dialog-examine">
import type { FormInstance, FormRules } from 'element-plus';
import { updateStationComment } from '@/service/apis/pile';

const ruleFormRef = ref<FormInstance>();

let dialogVisible = ref(false);

const optionsS = reactive({
  status: [] as any,
});

let ruleForm = reactive({
  id: '',
  content: '',
  spectacleStatus: '2',
  reply: '',
});

const rules = reactive<FormRules>({
  spectacleStatus: [
    { required: true, message: '请选择审核评论', trigger: 'blur' },
  ],
});

const show = (item: any, arr: any) => {
  ruleForm.id = item.id;
  ruleForm.content = item.content;
  ruleForm.spectacleStatus = '2';
  ruleForm.reply = '';
  if (item.replyStatus === 2) {
    ruleForm.reply = item.reply || '';
  }
  optionsS.status = arr.filter((item: any) => item.value !== '1');
  dialogVisible.value = true;
};
const hide = () => {
  dialogVisible.value = false;
};

const emit = defineEmits(['get-data']);
const onSubmit = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      const form: any = {
        id: ruleForm.id,
        spectacleStatus: ruleForm.spectacleStatus,
      };
      if (ruleForm.reply) {
        form.reply = ruleForm.reply;
        form.replyStatusName = '2';
      }
      updateStationComment(form).then((res: any) => {
        hide();
        emit('get-data');
      });
    } else {
      console.log('error submit!', fields);
    }
  });
};

defineExpose({
  show,
});
</script>